package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Url;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.*;

@Repository("UrlDAO")
@Transactional
public class UrlDAOImpl extends AbstractJpaDao<Url> implements UrlDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Url.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public UrlDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Url findUrlById(Integer id) throws DataAccessException {

		return findUrlById(id, -1, -1);
	}

	@Transactional
	public Url findUrlById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findUrlById", startResult, maxRows, id);
			return (Url) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Url> findUrlByName(String name) throws DataAccessException {

		return findUrlByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Url> findUrlByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUrlByName", startResult, maxRows, name);
		return new LinkedHashSet<Url>(query.getResultList());
	}

	@Transactional
	public Set<Url> findUrlByNameContaining(String name) throws DataAccessException {

		return findUrlByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Url> findUrlByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUrlByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Url>(query.getResultList());
	}

	@Transactional
	public Set<Url> findUrlByTitle(String title) throws DataAccessException {

		return findUrlByTitle(title, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Url> findUrlByTitle(String title, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUrlByTitle", startResult, maxRows, title);
		return new LinkedHashSet<Url>(query.getResultList());
	}

	@Transactional
	public Set<Url> findUrlByTitleContaining(String title) throws DataAccessException {

		return findUrlByTitleContaining(title, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Url> findUrlByTitleContaining(String title, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUrlByTitleContaining", startResult, maxRows, title);
		return new LinkedHashSet<Url>(query.getResultList());
	}

	@Transactional
	public Set<Url> findUrlByDate(Calendar date) throws DataAccessException {

		return findUrlByDate(date, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Url> findUrlByDate(Calendar date, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUrlByDate", startResult, maxRows, date);
		return new LinkedHashSet<Url>(query.getResultList());
	}

	@Transactional
	public Set<Url> findUrlByValue(String value) throws DataAccessException {

		return findUrlByValue(value, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Url> findUrlByValue(String value, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUrlByValue", startResult, maxRows, value);
		return new LinkedHashSet<Url>(query.getResultList());
	}

	@Transactional
	public Set<Url> findUrlByValueContaining(String value) throws DataAccessException {

		return findUrlByValueContaining(value, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Url> findUrlByValueContaining(String value, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUrlByValueContaining", startResult, maxRows, value);
		return new LinkedHashSet<Url>(query.getResultList());
	}

	@Transactional
	public Set<Url> findUrlByContent(String content) throws DataAccessException {

		return findUrlByContent(content, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Url> findUrlByContent(String content, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUrlByContent", startResult, maxRows, content);
		return new LinkedHashSet<Url>(query.getResultList());
	}

	@Transactional
	public Set<Url> findUrlByContentContaining(String content) throws DataAccessException {

		return findUrlByContentContaining(content, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Url> findUrlByContentContaining(String content, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findUrlByContentContaining", startResult, maxRows, content);
		return new LinkedHashSet<Url>(query.getResultList());
	}

	@Transactional
	public Set<Url> findUrlByDescription(String description) throws DataAccessException {

		return findUrlByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Url> findUrlByDescription(String description, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUrlByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Url>(query.getResultList());
	}

	@Transactional
	public Set<Url> findUrlByDescriptionContaining(String description) throws DataAccessException {

		return findUrlByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Url> findUrlByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findUrlByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Url>(query.getResultList());
	}

	@Transactional
	public Url findUrlByPrimaryKey(Integer id) throws DataAccessException {
		return findUrlByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Url findUrlByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findUrlByPrimaryKey", startResult, maxRows, id);
			return (Url) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Url> findUrlsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findUrlsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Url> findAllUrls() throws DataAccessException {

		return findAllUrls(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Url> findAllUrls(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllUrls", startResult, maxRows);
		return new LinkedHashSet<Url>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Url> findUrlsBySite(Integer siteId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllUrlsBySite", start, max, siteId);
		return new LinkedHashSet<Url>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Url> findUrlsByAuthor(Integer userId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllUrlsByAuthor", start, max, userId);
		return new LinkedHashSet<Url>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Url> findUrlByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Url>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Url> findUrlsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Url.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Url> urls = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return urls;
	}

	@Transactional
	public Url findUrlByPhysicalPrimaryKey(String value) throws DataAccessException {
		return findUrlByPhysicalPrimaryKey(value, -1, -1);
	}

	@Transactional
	public Url findUrlByPhysicalPrimaryKey(String value, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findUrlByPhyscialPrimaryKey", startResult, maxRows, value);
			return (Url) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Url entity) {
		return true;
	}
}
